package com.vipsoft.base.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 
 */
public class ZipUtils {
    private static final Logger logger = LoggerFactory.getLogger(ZipUtils.class);

    /**
     * gZip压缩方法
     */
    public static byte[] gZip(byte[] data) {
        byte[] b = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(bos);
            gzip.write(data);
            gzip.finish();
            gzip.close();
            b = bos.toByteArray();
            bos.close();
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
        return b;
    }

    /**
     * gZip解压方法
     */
    public static byte[] unGZip(byte[] data) {
        byte[] b = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
            GZIPInputStream gzip = new GZIPInputStream(bis);
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = gzip.read(buf, 0, buf.length)) != -1) {
                baos.write(buf, 0, num);
            }
            b = baos.toByteArray();
            baos.flush();
            baos.close();
            gzip.close();
            bis.close();
        } catch (Exception ex) {
            logger.debug(ex.getMessage(), ex);
        }
        return b;
    }

    public static String gZip(String rawText) {
        String result = "";
        try {
            byte[] zipBuffer = gZip(rawText.getBytes("UTF-8"));
            BASE64Encoder encoder = new BASE64Encoder();
            result = encoder.encode(zipBuffer);
        } catch (IOException ex) {
            logger.error(rawText);
            logger.error(ex.getMessage(), ex);
        }
        return result;
    }

    /**
     * 解压字符串，如果解压失败，使用原字符串（考虑部分加密问题）
     *
     * @param zipText 待解压字符串
     * @return
     */
    public static String unGZip(String zipText) {
        String result = "";
        try {
            BASE64Decoder decoder = new BASE64Decoder();
            byte[] buffer = decoder.decodeBuffer(zipText);

            byte[] unzipBuffer = unGZip(buffer);
            result = new String(unzipBuffer, "UTF-8");
        } catch (IOException ex) {
            result = zipText;
            logger.error("unz error："+zipText);
            logger.debug(ex.getMessage(), ex);
        }
        return result;
    }
}
